package br.edu.ufcg.embedded.eolica.view;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

import br.edu.ufcg.embedded.eolica.MIDlet.UsinaEolicaMIDlet;
import br.edu.ufcg.embedded.eolica.connection.GraphConnection;
import br.edu.ufcg.embedded.eolica.domain.User;
import br.edu.ufcg.embedded.eolica.util.Base64;
import br.edu.ufcg.embedded.eolica.util.MyCallback;
import br.edu.ufcg.embedded.eolica.util.Properties;
import br.edu.ufcg.embedded.eolica.webservices.ArrayOfString;

public class GraphForm extends Form implements CommandListener, MyCallback {

	private Display d;
	private Displayable previous;
	private Command cmdNewConsult;
	private Properties prop;
	public static String WIND_GRAPH = "1";
	public static String STATISTICAL_GRAPH = "2";
	private int loadIndex;

	public GraphForm(Display d, Displayable previous, String graph) {
		super("Eletrobras - " + graph);
		this.d = d;
		this.previous = previous;

		prop = UsinaEolicaMIDlet.getProp();
		cmdNewConsult = new Command(prop.getProperty("label.newconsult"),
				Command.BACK, 0);
		addCommand(cmdNewConsult);
		setCommandListener(this);
	}

	public void commandAction(Command cmd, Displayable arg1) {
		if (cmd == cmdNewConsult) {
			deleteAll();
			d.setCurrent(previous);
		}

	}

	public void getGraph(User user, String station, int height, long initDateTime, long finalDateTime,	String graphType) {
/*		System.out.println(this + user.getUsername() + user.getPassword() +
				station + "h: " + height + "p: " + period + " - " + graphType);*/
		new Thread(new GraphConnection(this, user.getUsername(), user
				.getPassword(), station, height, initDateTime, finalDateTime, graphType, getWidth(), getHeight())).start();
		
		loadIndex = this.append(prop.getProperty("text.load"));
	}

	public void fire(Object obj) {
		if (obj instanceof ArrayOfString) {
			ArrayOfString array = (ArrayOfString) obj;
			String[] images = array.getString();
			int l = images.length;
			String image;
			byte[] imageBytes;

			for (int i = 0; i < l; i++) {
				image = images[i];
				if (image.length() > 0) {
					imageBytes = Base64.decode(image);
					append(Image.createImage(imageBytes, 0, imageBytes.length));
					//append(resizeImage(Image.createImage(imageBytes, 0, imageBytes.length)));
				}
			}
		} else if (obj instanceof Exception){
			Exception obj2 = (Exception) obj;
			obj2.printStackTrace();
			d.setCurrent(new ErrorAlert(obj2.toString(), obj2.getMessage()), previous);
			//d.setCurrent(new ErrorAlert(prop.getProperty("label.error"), prop.getProperty("error.loadgraph")), previous);
		}
		delete(loadIndex);
	}

/*	private Image resizeImage(Image src) {
		int srcWidth = src.getWidth();
		int srcHeight = src.getHeight();
		Image tmp = Image.createImage(getWidth(), getHeight());
		Graphics g = tmp.getGraphics();
		int ratio = (srcWidth << 16) / getWidth();
		int pos = ratio / 2;

		// Horizontal Resize

		for (int x = 0; x < getWidth(); x++) {
			g.setClip(x, 0, 1, srcHeight);
			g.drawImage(src, x - (pos >> 16), 0, Graphics.LEFT | Graphics.TOP);
			pos += ratio;
		}

		Image resizedImage = Image.createImage(getWidth(), getHeight());
		g = resizedImage.getGraphics();
		ratio = (srcHeight << 16) / getHeight();
		pos = ratio / 2;

		// Vertical resize

		for (int y = 0; y < getHeight(); y++) {
			g.setClip(0, y, getWidth(), 1);
			g.drawImage(tmp, 0, y - (pos >> 16), Graphics.LEFT | Graphics.TOP);
			pos += ratio;
		}
		return resizedImage;

	}// resize image
*/
}
